const express = require('express')
const cors = require('cors')
const app = express()
// 允许跨域
app.use(cors())

// 此中间件获取post请求体参数
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.get('/music', (req, res) => {
  let { id } = req.query
  let musics = [
    { id: 1, musicName: '月光唉' },
    { id: 2, musicName: '约会' },
    { id: 3, musicName: '她离开我的时候' },
  ]
  if (id) {
    let music = musics.find((item) => item.id === parseInt(id))
    res.json(music ? music : {})
  } else {
    res.json(musics)
  }
})

app.post('/login', (req, res) => {
  let { username, password } = req.body
  if (username === 'erqiao' && password === '20040228') {
    res.json({ msg: '登入成功' })
  } else {
    res.json({ msg: '账号或密码错误' })
  }
})

app.listen(6001, (req, res) => {
  console.log('服务器启动了，端口为6000')
})
